<?php
/**
 * FlickrPhotosetsUpdate class file
 * @package FlickrPhotosets
 */

/**
 * Performs the necessary steps to update or remove a user's profile.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 7.10.29
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 * @since Class available since Release 2.0
 */
class FlickrPhotosetsUpdate extends FlickrPhotosets {
	
	/**
	 * Sets the session key to that of the developer to ensure that it
	 * is infinite. Determines whether to update or remove the profile.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct() {
		parent::__construct();
		
		/*
		Create an object of the REST client so we can use the
		developer's session key. This ensures that we are always
		using an infinite session key.
		*/
		$facebook->api_client->session_key = $this->get_developer_sessionkey();
		
		// Set the UID
		$this->uid = $this->facebook->user;
		
		// Handle different updating
		if (isset($_GET['remove'])) {
			$this->remove_profile();
		}
		else {
			$this->update_profile();
		}
	}
	
	/**
	 * Remove the profile by sending NULL and redirect to the config page.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function remove_profile() {
		$this->facebook->api_client->profile_setFBML(NULL, $this->uid);
		$this->facebook->redirect($this->app_url.'/configure.php?profileremoved');
	}
	
	/**
	 * Set the user's profile FBML to the default and refresh the ref url.
	 * Then redirect to either the profile or configuration page.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function update_profile() {
		
		/*
		Old Method using fb:ref
		This is too inconsistent. It works for the majority of users, but some
		have issues updating the profile. Until Facebook fixes the issue, we
		will just set the fbml directly instead of using fb:ref.
		
		$this->set_profile_fbml(true);
		$this->facebook->api_client->fbml_refreshRefUrl($this->app_call_url.'/profile.php?handle='.$this->uid);
		*/
		$this->set_profile_fbml();
		
		// Redirect based on where the user came from
		if (isset($_GET['fromprofile'])) {
			$this->log->update($this->uid, 'fromprofile');
			$this->facebook->redirect($this->facebook->get_facebook_url().'/profile.php?id='.$this->uid);
		}
		else {
			$this->log->update($this->uid);
			$this->facebook->redirect($this->app_url.'/configure.php?profileupdated');
		}
		
	}
	
}

?>